/*----------------------------------------------------------------------------
 * MainTopMenu
 * ---------------------------------------------------------------------------
 * Date      : 2011-02-08
 * Author    : naiyumie
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 *
 * ---------------------------------------------------------------------------
 * EXAMPLE
 * ---------------------------------------------------------------------------
 *
 *----------------------------------------------------------------------------
 */
$.fn.MainTopMenu	= function (option)
{
	var element		= $(this);
	var option		= option || {};
	var interval	= option.interval ? option.interval : 300;
	var timeout		= null;

	if (!option.hover) { option.hover	= {};}

	var over	= function(obj)
	{
		var index	= $(obj).parent().index();

		$(element).addClass('lnbbg2');
		$(element).children('ul.lnb-menu').children('li').find('div:first').css({'visibility':'visible'}).show();

		if (index>-1)
		{
			$(element).children('ul.lnb-menu').children('li:first').children('div:first').css('background', 'url("../../images/main2/navi/bg_navi'+(index+1)+'.jpg") no-repeat 0 18px');
		}
	};

	var out		= function ()
	{
		$(element).removeClass('lnbbg2');
		$(element).children('ul.lnb-menu').children('li').find('div:first').css({'visibility':'visible'}).hide();
	};

	var timer_in	= function (obj)
	{
		clearTimeout(timeout);
		over(obj);
	};

	var timer_out	= function ()
	{
		var set_time_out	= function () { out(); };
		timeout	= setTimeout(set_time_out, interval);
	};

	var changeImage	= function (obj, ext, action)
	{
		var img	= $(obj).find('img');
		var src	= $(img).attr('src');

		if (action=='over')
		{
			src	= src.replace('_on' + ext, ext);
			src	= src.replace(ext, '_on' + ext);
		}
		else { src	= src.replace('_on' + ext, ext); }

		$(img).attr('src', src);
	};

	$(element).children('ul.lnb-menu').children('li').find('a:first')
	.hover
	(
		function()
		{
			changeImage($(this), '.gif', 'over');
			timer_in($(this));
		},
		function()
		{
			changeImage($(this), '.gif');
			timer_out();
		}
	)
	.focus(function()
	{
		changeImage($(this), '.gif', 'over');
		timer_in($(this));
	})
	.blur(function()
	{
		changeImage($(this), '.gif');
		timer_out();
	});

	$(element).find('ul.lnb-sub')
	.hover
	(
		function() { timer_in($(this).parent().parent().children('a:first')); },
		function() { timer_out(); }
	);

	$(element).find('ul.lnb-sub').find('a')
	.focus(function() { timer_in($(this).parent().parent().parent().parent().children('a:first')); })
	.blur(function() { timer_out(); });

	$(element).find('ul.lnb-sub').find('a').each(function()
	{
		$(this).hover
		(
			function() { changeImage($(this), '.gif', 'over'); },
			function() { changeImage($(this), '.gif'); }
		);
	});

};
